package com.qzboot.backend.web.module.sys.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.qzboot.backend.web.common.config.SysLog;
import com.qzboot.backend.web.module.sys.BaseSysController;
import com.qzboot.common.support.WebResult;
import com.qzboot.facade.entity.SysMenu;
import com.qzboot.facade.service.SysMenuService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author zengzhihong
 * @date 2019-12-16
 */
@RestController
@RequestMapping("/sysMenu")
public class SysMenuController extends BaseSysController {

    @Reference
    private SysMenuService sysMenuService;

    /**
     * 查询所有菜单
     *
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("sys:menu:list")
    public WebResult list() {
        //这里是返回菜单
        return WebResult.success(CollectionUtil.sortByProperty(sysMenuService.list(), "sortNum"));
    }

    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @SysLog("新增菜单")
    @PostMapping("/save")
    @RequiresPermissions("sys:menu:save")
    public WebResult save(@Valid @RequestBody SysMenu entity) {
        sysMenuService.save(entity);
        return WebResult.success();
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    @SysLog("修改菜单")
    @PostMapping("/updateById")
    @RequiresPermissions("sys:menu:update")
    public WebResult updateById(@Valid @RequestBody SysMenu entity) {
        sysMenuService.updateById(entity);
        return WebResult.success();
    }


    @SysLog("删除菜单")
    @GetMapping("/removeById")
    @RequiresPermissions("sys:menu:delete")
    public WebResult removeById(Long id) {
        sysMenuService.removeById(id);
        return WebResult.success();
    }

}
